//
//  PersonalTableViewController.m
//  Weiweiyunshi
//
//  Created by 张旭峰 on 2017/2/5.
//  Copyright © 2017年 王译. All rights reserved.
//

#import "PersonalTableViewController.h"
#import "imageTableViewCell.h"
#import "imageTableViewCell.h"
#import "QRCodeTableViewCell.h"
#import "PersonalImageViewController.h"
#import "PersonQRCodeController.h"
@interface PersonalTableViewController ()<UITableViewDelegate,UITableViewDataSource>


@end

@implementation PersonalTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.navigationItem.title = @"个人";
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"header_back_icon"] style:UIBarButtonItemStyleDone target:self action:@selector(pop)];
    self.navigationItem.leftBarButtonItem = backItem;
     [self setupUI];
    self.tableView.rowHeight=UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight=200;
    [self.tableView registerNib:[UINib nibWithNibName:@"imageTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell99"];
     [self.tableView registerNib:[UINib nibWithNibName:@"QRCodeTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell88"];
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell2"];
    self.tableView.delegate=self;

   
//    [self loadData];
    
}
-(void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)setupUI
{
    UITableView* tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    // 设置数据源对象
    tableView.dataSource = self;
    // 把tableView添加到根视图上
    [self.view addSubview:tableView];
    // self.tableView.contentOffset=CGPointMake(0, -100);
    
    self.tableView = tableView;

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section ==0)
    {
        return 3;
    }else
    {
        return 4;
    }
}



-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
    if (cell== nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell2"];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    }
    if(indexPath.section ==0)
    {
    
   
        switch (indexPath.row){
            case 0:
            {
//                cell.textLabel.text = @"头像";
//                cell.detailTextLabel.text=self.Model.name;
//                return cell;
                
                imageTableViewCell *cell0=[tableView dequeueReusableCellWithIdentifier:@"cell99" forIndexPath:indexPath];
                //
                cell0.leftlabel.text = @"头像";
//                [cell0.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.Model.img]];
                cell0.rightImageView.image = self.globalImage;
                cell0.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                return cell0;
            }
            case 1:
            {
                cell.textLabel.text = @"姓名";
                cell.detailTextLabel.text=self.Model.name;
                 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

                 return cell;
            }
            case 2:
            {
//                cell.textLabel.text = @"二维码名片";
//                cell.detailTextLabel.text=self.Model.name;
//
//                 return cell;
                
                
                QRCodeTableViewCell *cell88=[tableView dequeueReusableCellWithIdentifier:@"cell88" forIndexPath:indexPath];
                cell88.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                return cell88;

            }
            default:
                break;
                
        }
        
    }
    else{
      UITableViewCell * cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
    
    //如果队列中没有该类型cell，则会返回nil，这个时候就需要自己创建一个cell
       if (cell1== nil) {
        
        cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell2"];
         cell1.detailTextLabel.font = [UIFont systemFontOfSize:13];
         }
//        return cell1;
        switch (indexPath.row) {
            case 0:
            {
                cell1.textLabel.text = @"姓名";
                cell1.detailTextLabel.text = self.Model.name;
                return cell1;
            }
            case 1:
            {
                cell1.textLabel.text = @"电话";
                cell1.detailTextLabel.text = self.Model.mobile;
                 return cell1;
            }
            case 2:
            {
                cell1.textLabel.text = @"电子邮箱";
                cell1.detailTextLabel.text = self.Model.email;
                 return cell1;
            }
            case 3:
            {
                cell1.textLabel.text = @"部门";
                cell1.detailTextLabel.text = self.Model.dept;
                 return cell1;
            }
                
            default:
                break;
        }
        
    }
    
    return cell;
    
}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section==0)
    {
        return @"个人信息";
    }else
    {
        return @"企业信息";
        
    }
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section ==0)
    {
         return 30;
    }else
    {
        return 15;
    };
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    
    //     .accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 0 && indexPath.row ==0)
    {
        PersonalImageViewController * VC5 = [[PersonalImageViewController alloc]init];
        VC5.imageblock = ^(UIImage * image){
            
            imageTableViewCell * cell000 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            cell000.rightImageView.image = image;
            self.globalImage = image;
            
        };
        
        VC5.image = self.globalImage;
        //        [self presentModalViewController:VC5 animated:NO];
        
        [self.navigationController pushViewController:VC5 animated:NO];
        //        [self presentViewController:VC5 animated:YES completion:^{
        //
        //        }];
        
    }else if(indexPath.section==0 && indexPath.row ==2){
        
        PersonQRCodeController * VC6 = [[PersonQRCodeController alloc]init];
        VC6.Model = self.Model;
//        VC6.image = self.globalImage;
//        [VC6.titleImage setImage:_globalImage];
//        UIImageView *images =[[UIImageView alloc]initWithImage:self.globalImage];
//        [VC6 setTitleImage:images];
//        [VC6.name setText:@"nihao"];
        VC6.titleImage = self.globalImage;
        NSLog(@"----VC6----->%@",VC6.Model);
        NSLog(@"----VC6----->%@",self.Model);
        
        [self.navigationController pushViewController:VC6 animated:NO];
        
        
    }else
    {
        
    }
    

}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.imageblock1(self.globalImage);
}

@end
